#ifndef __PROTOCOL_485_PARSE_PACK_H__
#define __PROTOCOL_485_PARSE_PACK_H__
#include <typescommon.h>

enum protocolParseCommonStepEnum
{
    protocolParseCommonStep_FixedHead = 0,
    protocolParseCommonStep_Version,
    protocolParseCommonStep_AllHead,
    protocolParseCommonStep_Data,
};
enum protocolParseCommonVersionEnum
{
    protocolParseCommonVersion0 = 0,
    protocolParseCommonVersion1,
    protocolParseCommonVersion2,
    protocolParseCommonVersion3,
};
enum protocolParseCrypteTypeEnum
{
    protocolParseCrypte_None = 0,
    protocolParseCrypte_Aes,
    protocolParseCrypte_Rsa,
    protocolParseCrypte_SM4,
    protocolParseCrypte_SM2,
    protocolParseCrypte_Des3,
    protocolParseCrypte_AesPre,
    protocolParseCrypte_SM4Pre,
    //
};
// PTC:protocol
#define PTC_PACK_BEGIN_PST 0 //begin position
#define PTC_PACK_VER_PST 3 // version position
#define PTC_PACK_VER_HEAD_PST 4 // version head position
// VERSION 1
#define PTC_PACK_V1_SIZE_PST 4 // version 1 size position
#define PTC_PACK_V1_ADDR_PST 6 // version 1 addr position
#define PTC_PACK_V1_ENCYPT_PST 10 // version 1 encrypte position
#define PTC_PACK_V1_ADDR_SIZE (PTC_PACK_V1_ENCYPT_PST - PTC_PACK_V1_ADDR_PST) // version 1 addr SIZE
#define PTC_PACK_V1_CRC_PST 11 // version 1 encrypte position
#define PTC_PACK_V1_DATA_PST 12 // version 1 data position
//VERSION 2

typedef uint16_t ptcPackType;
struct ptcPackOption
{
    uint8_t *pkptr;
    void *userdata;
    ptcPackType pksize;
    ptcPackType rcvCount;
    ptcPackType needSize;
    uint8_t parseState;
    uint8_t myVersion;
};
int ptcPackOptInit(struct ptcPackOption *parseIstc, void *setpack, ptcPackType setPackSize, void *udata, uint8_t setver);
void ptcPackParseData(struct ptcPackOption *parseIstc, uint8_t indata);
void ptcPackParseCbk(struct ptcPackOption *parseIstc);

ptcPackType ptcPackGetHeadSize(struct ptcPackOption *parseIstc);
ptcPackType ptcPackGetTotalSize(struct ptcPackOption *parseIstc);
ptcPackType ptcPackGetRemainSize(struct ptcPackOption *parseIstc);

uint8_t ptcPackGetVersion(struct ptcPackOption *parseIstc);
int ptcPackSetVersion(struct ptcPackOption *parseIstc, uint8_t setCrypte);

uint16_t ptcPackGetDataSize(struct ptcPackOption *parseIstc);
int ptcPackSetDataSize(struct ptcPackOption *parseIstc, uint16_t setsize);

const uint8_t *ptcPackGet485Addr(struct ptcPackOption *parseIstc);
int ptcPackSet485Addr(struct ptcPackOption *parseIstc, const uint8_t *setAddr);

int ptcPackCheckAddr(struct ptcPackOption *parseIstc, const uint8_t *myaddr);

uint8_t ptcPackGetCrypte(struct ptcPackOption *parseIstc);
int ptcPackSetCrypte(struct ptcPackOption *parseIstc, uint8_t setCrypte);

uint8_t *ptcPackGetUsrData(struct ptcPackOption *parseIstc);
int ptcPackSetUsrData(struct ptcPackOption *parseIstc, const uint8_t *usrData, ptcPackType dsize);

uint8_t ptcPackGetCrcU8(struct ptcPackOption *parseIstc);
int ptcPackSetCrcU8(struct ptcPackOption *parseIstc, uint8_t setCrc);

ptcPackType ptcPackFinishData(struct ptcPackOption *parseIstc);


#endif
